package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.ReservaServicioDao;
import edu.upc.clase.demo.entity.ReservaServicio;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;
/**
 *
 * @author NICOLAS
 */
@Repository
public class ReservaServicioDaoImpl extends SimpleJdbcDaoSupport implements ReservaServicioDao {
    private static Logger log = LoggerFactory.getLogger(ReservaServicioDaoImpl.class);
    
    @Autowired
    public ReservaServicioDaoImpl(DataSource dataSource) 
    {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }
     
    @Override
    public Integer insertar(ReservaServicio reservaservicio) 
    {
        try {
        getJdbcTemplate().update(
        "insert into reserva_servicio (coreserva, coservicio, fluso) values (?,?,?)",
        reservaservicio.getReserva().getCoreserva(),
        reservaservicio.getServicio().getCoservicio(), 
        reservaservicio.getFluso());
        return 1;
        } catch (Exception e) {
            log.error("error al insertar");
        }
        return null;
    }    
    
    @Override
    public void actualizar(ReservaServicio reservaservicio)
    {
        getJdbcTemplate().update(
                "update reserva_servicio set fluso = ? where coreserva = ? and coservicio = ?",
                reservaservicio.getFluso(), reservaservicio.getReserva().getCoreserva(), 
                reservaservicio.getServicio().getCoservicio());
    }
    
    @Override
    public void eliminar(ReservaServicio reservaservicio)
    {
        getJdbcTemplate().update(
                "delete from reserva_servicio where coreserva = ? and coservicio = ?", 
                reservaservicio.getReserva().getCoreserva(), reservaservicio.getServicio().getCoservicio());
    }
    
    @Override
    public ReservaServicio buscar(Integer coreserva, Integer coservicio)
    {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select coreserva,coservicio,fluso from reserva_servicio where coreserva=? and coservicio=?",
                    new BeanPropertyRowMapper<ReservaServicio>(ReservaServicio.class), coreserva, coservicio);
        } catch (EmptyResultDataAccessException e) {
            return null;
        } catch (Exception e) {
            return null;
        }
    }
}
